<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @package		Libraries
 * @author		Glen.luo
 * @since		Version 1.0
 */

// ------------------------------------------------------------------------

/**
 * Template Layout Class
 *
 * A layout defines the base look for your website. It is the foundation that
 * your other views get rendered in. They make it easy to provide consistent
 * look throughout your application.
 *
 */
class Layout {

	private $CI = NULL;

	/**
	 * Constructor
	 *
	 * Loads the calendar language file and sets the default time reference
	 */
	public function __construct() {
		$this->CI =& get_instance();
		log_message('debug', "Layout Class Initialized");
    }

    public function load($vars, $view = NULL, $return = FALSE)
    {
        if (empty($view)) {
            $view = $this->CI->uri->segment(1) . '/' . $this->CI->uri->segment(1) . '.php';
        }

        if (file_exists( APPPATH . "views/$view" ) === FALSE) {
            show_error("Unable to load the template file: views/{$view}");
		    log_message('error', "需要加载的模板文件：" . APPPATH . "views/$view 不存在");
        }
        else {
            $body = $this->CI->load->view($view, $vars, TRUE);

            if ($vars === NULL) {
                $vars = array('body' => $body);
            }
            elseif (is_array($vars) === TRUE) {
                if (array_key_exists('body', $vars)) {
                    $vars['ren_body'] = $var['body'];
                }
                $var['body'] = $body;
            }
            elseif (is_object($vars) === TRUE) {
                if (property_exists($vars, 'body') === TRUE) {
                    $vars->ren_body = $vars->body;
                }
                $vars->body = $body;
            }
        }

        if (!empty($this->CI->layout)) {
            $layout = "layout/{$this->CI->layout}.php";
        }
        else {
            $layout = "layout/default.php";
        }

        if (file_exists( APPPATH . "views/{$layout}" ) === FALSE) {
            show_error("Unable to load the template layout file: views/{$layout}");
            log_message('error', "需要加载的布局文件：" .  APPPATH . "views/{$layout} 不存在");
        }

        return $this->CI->load->view($layout, $vars, $return);
    }
}

// END Layout class

/* End of file Layout.php */
/* Location: ./com_patry/libraries/Layout.php */
